package com.yunxin.core.utils.infra.utils;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import com.yunxin.core.utils.infra.common.constant.CommonConstants;
import com.yunxin.core.utils.infra.common.enums.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author zero°
 * @since 2023-12-14
 */
public enum AttributeDetermineUtils {
    /**
     * 实例
     */
    X;

    /**
     * 年龄判断
     *
     * @param minAge minAge
     * @param maxAge maxAge
     * @param age    age
     * @return boolean
     */
    public boolean isAgeResult(Integer minAge, Integer maxAge, Integer age) {
        boolean ageResult = true;
        if (minAge != null && maxAge != null) {
            if (age == null) {
                return false;
            }
            boolean minAgeResult = minAge < age;
            boolean maxAgeResult = age < maxAge;
            ageResult = minAgeResult && maxAgeResult;
        } else {
            if (minAge != null) {
                if (age == null) {
                    return false;
                }
                ageResult = minAge < age;
            } else if (maxAge != null) {
                if (age == null) {
                    return false;
                }
                ageResult = age < maxAge;
            }
        }
        return ageResult;
    }

    /**
     * 属性判断(= 教育程度、芝麻分、职业、职业收入、性别、逾期)
     *
     * @param attribute        attribute
     * @param configAttributes configAttributes
     * @return boolean
     */
    public boolean isResult(Integer attribute, String configAttributes) {
        // 产品属性未开启
        if (StringUtils.isBlank(configAttributes)) {
            return true;
        }
        // 产品属性数组
        List<Integer> productAttributeList = ConvertUtils.X.toIntegerList(configAttributes, CommonConstants.COMMA);
        // 产品属性无要求
        if (productAttributeList.contains(0)) {
            return true;
        }
        // 为空则意味着没填,默认0,除非产品属性包含无要求,否则都是false
        if (attribute == null) {
            attribute = 0;
        }
        return productAttributeList.contains(attribute);
    }

    /**
     * 额度属性判断(= 京东白条、蚂蚁花呗、微粒贷)
     *
     * @param quotaAttribute        quotaAttribute
     * @param configQuotaAttributes configQuotaAttributes
     * @return boolean
     */
    public boolean isQuotaResult(Integer quotaAttribute, String configQuotaAttributes) {
        // 产品额度属性未开启
        if (StringUtils.isBlank(configQuotaAttributes)) {
            return true;
        }
        // 产品额度属性数组
        List<Integer> productQuotaAttributeList = ConvertUtils.X.toIntegerList(configQuotaAttributes, CommonConstants.COMMA);
        // 产品额度属性无要求
        if (productQuotaAttributeList.contains(0)) {
            return true;
        }
        // 为空则意味着没填,默认5-无
        if (quotaAttribute == null) {
            quotaAttribute = 5;
        }
        return productQuotaAttributeList.contains(quotaAttribute);
    }

    /**
     * 社保判断
     *
     * @param socialSecurity       socialSecurity
     * @param configSocialSecurity configSocialSecurity
     * @return boolean
     */
    public boolean isSocialSecurityResult(Integer socialSecurity, String configSocialSecurity) {
        // 产品社保属性未开启
        if (StringUtils.isBlank(configSocialSecurity)) {
            return true;
        }
        // 产品社保属性数组
        List<Integer> productSocialSecurityList = ConvertUtils.X.toIntegerList(configSocialSecurity, CommonConstants.COMMA);
        // 产品社保属性无要求
        if (productSocialSecurityList.contains(SocialSecurityLevelEnum.NO_REQUIRED.getValue())) {
            return true;
        }
        // 为空则意味着没填,默认无社保
        if (socialSecurity == null) {
            socialSecurity = SocialSecurityLevelEnum.NO_SOCIAL_SECURITY.getValue();
        }
        return productSocialSecurityList.contains(socialSecurity);
    }

    /**
     * 公积金判断
     *
     * @param accumulationFund        accumulationFund
     * @param configAccumulationFunds configAccumulationFunds
     * @return boolean
     */
    public boolean isAccumulationFundResult(Integer accumulationFund, String configAccumulationFunds) {
        // 产品公积金属性未开启
        if (StringUtils.isBlank(configAccumulationFunds)) {
            return true;
        }
        // 产品社保属性数组
        List<Integer> productAccumulationFundList = ConvertUtils.X.toIntegerList(configAccumulationFunds, CommonConstants.COMMA);
        // 产品公积金属性无要求
        if (productAccumulationFundList.contains(AccumulationFundLevelEnum.NO_REQUIRED.getValue())) {
            return true;
        }
        // 为空则意味着没填,默认无公积金
        if (accumulationFund == null) {
            accumulationFund = AccumulationFundLevelEnum.NO_ACCUMULATION_FUND.getValue();
        }
        return productAccumulationFundList.contains(accumulationFund);
    }


    /**
     * 信用卡判断
     *
     * @param creditCard        creditCard
     * @param configCreditCards configCreditCards
     * @return boolean
     */
    public boolean isCreditCardResult(Integer creditCard, String configCreditCards) {
        // 产品信用卡属性未开启
        if (StringUtils.isBlank(configCreditCards)) {
            return true;
        }
        // 产品信用卡属性数组
        List<Integer> productCreditCardList = ConvertUtils.X.toIntegerList(configCreditCards, CommonConstants.COMMA);
        // 产品信用卡属性无要求
        if (productCreditCardList.contains(CreditCardEnum.NO_REQUIRED.getValue())) {
            return true;
        }
        // 为空则意味着没填,默认无信用卡
        if (creditCard == null) {
            creditCard = CreditCardEnum.NO.getValue();
        }
        return productCreditCardList.contains(creditCard);
    }


    /**
     * 房产判断
     *
     * @param house        house
     * @param configHouses configHouses
     * @return boolean
     */
    public boolean isHouseResult(Integer house, String configHouses) {
        // 产品房产属性未开启
        if (StringUtils.isBlank(configHouses)) {
            return true;
        }
        // 产品房产属性数组
        List<Integer> productHouseList = ConvertUtils.X.toIntegerList(configHouses, CommonConstants.COMMA);
        // 产品房产属性无要求
        if (productHouseList.contains(HouseLevelEnum.NO_REQUIRED.getValue())) {
            return true;
        }
        // 为空则意味着没填,默认无房产
        if (house == null) {
            house = HouseLevelEnum.NO_HOUSE.getValue();
        }
        return productHouseList.contains(house);
    }

    /**
     * 车产判断
     *
     * @param vehicle        vehicle
     * @param configVehicles configVehicles
     * @return boolean
     */
    public boolean isVehicleResult(Integer vehicle, String configVehicles) {
        // 产品车产属性未开启
        if (StringUtils.isBlank(configVehicles)) {
            return true;
        }
        // 产品车产属性数组
        List<Integer> productVehicleList = ConvertUtils.X.toIntegerList(configVehicles, CommonConstants.COMMA);
        // 产品车产属性无要求
        if (productVehicleList.contains(VehicleLevelEnum.NO_REQUIRED.getValue())) {
            return true;
        }
        // 为空则意味着没填,默认无车产
        if (vehicle == null) {
            vehicle = VehicleLevelEnum.NO_VEHICLE.getValue();
        }
        return productVehicleList.contains(vehicle);
    }

    /**
     * 保险判断
     *
     * @param insure        insure
     * @param configInsures configInsures
     * @return boolean
     */
    public boolean isInsureResult(Integer insure, String configInsures) {
        // 产品保险属性未开启
        if (StringUtils.isBlank(configInsures)) {
            return true;
        }
        // 产品保险属性数组
        List<Integer> productInsureList = ConvertUtils.X.toIntegerList(configInsures, CommonConstants.COMMA);
        // 产品保险属性无要求
        if (productInsureList.contains(InsureEnum.NO_REQUIRED.getValue())) {
            return true;
        }
        // 为空则意味着没填,默认无保险
        if (insure == null) {
            insure = InsureEnum.NO_INSURE.getValue();
        }
        return productInsureList.contains(insure);
    }

    /**
     * 逾期判断
     *
     * @param beOverdue       beOverdue
     * @param configBeOverdue configBeOverdue
     * @return boolean
     */
    public boolean isBeOverdueResult(Integer beOverdue, String configBeOverdue) {
        // 产品逾期属性未开启
        if (StringUtils.isBlank(configBeOverdue)) {
            return true;
        }
        // 产品逾期属性数组
        List<Integer> productBeOverdueList = ConvertUtils.X.toIntegerList(configBeOverdue, CommonConstants.COMMA);
        // 产品逾期属性无要求
        if (productBeOverdueList.contains(BeOverdueEnum.NO_REQUIRED.getValue())) {
            return true;
        }
        // 为空则意味着没填,默认否
        if (beOverdue == null) {
            beOverdue = BeOverdueEnum.NO.getValue();
        }
        return productBeOverdueList.contains(beOverdue);
    }

    /**
     * 城市判断
     *
     * @param city             city
     * @param configCityConfig configCityConfig
     * @return boolean
     */
    public boolean isCityResult(String city, String configCityConfig) {
        //如果没有说明是全国
        if (CharSequenceUtil.isBlank(configCityConfig)) {
            return true;
        }
        //检测市
        String cover = AreasCheckUtil.checkAndCover(city);
        if (StrUtil.isBlank(cover)) {
            return false;
        }
        List<String> cityList = Arrays.asList(configCityConfig.split(","));
        return !cityList.contains(cover);
    }
}
